# -*- coding: utf-8 -*-
import unittest
import sys

sys.path.append('../')

from chemdb import dbHandle
from data import *

class GetBaseTestCase(unittest.TestCase):
	def setUp(self):
		from os import system
		system("mysql < ../chemdb.sql") # This clears the database

		self.compound1Id = dbHandle.insertCompound(compound1['name'], compound1['formula'], compound1['iupac'])
		self.compound2Id = dbHandle.insertCompound(compound2['name'], compound2['formula'], compound2['iupac'])
		self.compound3Id = dbHandle.insertCompound(compound3['name'], compound3['formula'], compound3['iupac'])
		dbHandle.insertInchi(self.compound1Id, compound1['inchi'])
		dbHandle.insertMol(self.compound1Id, compound1['molfile'])
		dbHandle.insertFingerprint(compound1['fp'], self.compound1Id)
		dbHandle.insertInchi(self.compound2Id, compound2['inchi'])
		dbHandle.insertMol(self.compound2Id, compound2['molfile'])
		dbHandle.insertFingerprint(compound2['fp'], self.compound2Id)
		dbHandle.insertInchi(self.compound3Id, compound3['inchi'])
		dbHandle.insertMol(self.compound3Id, compound3['molfile'])
		dbHandle.insertFingerprint(compound3['fp'], self.compound3Id)
		self.reference1Id = dbHandle.insertReference(reference1['name'], reference1['author'], reference1['year'], reference1['source'])
		self.reaction1Id = dbHandle.insertReaction(self.reference1Id, reaction1['temp'], reaction1['solvent'], reaction1['state'], reaction1['pressure'], reaction1['time'], reaction1['pH'], reaction1['other'])
		self.catalyst1Id = dbHandle.insertCatalyst(catalyst1['name'], self.reaction1Id)
		self.catalyst2Id = dbHandle.insertCatalyst(catalyst2['name'], self.reaction1Id)
		self.relation1Id = dbHandle.insertRelation(self.compound2Id, self.compound1Id, self.reaction1Id)
		self.relation2Id = dbHandle.insertRelation(self.compound3Id, self.compound1Id, self.reaction1Id)

	def tearDown(self):
		from os import system
		system("mysql < ../chemdb.sql") # This clears the database

#class GetMolTestCase(GetBaseTestCase):
	#def runTest(self):
		#molObj = dbHandle.getMol(self.compound1Id)
		#self.assertEqual(molfile, compound1['molfile'])

class GetInchiTestCase(GetBaseTestCase):
	def runTest(self):
		inchi = dbHandle.getInchi(self.compound1Id)
		self.assertEqual(inchi, compound1['inchi'])

class GetCompoundNameTestCase(GetBaseTestCase):
	def runTest(self):
		name = dbHandle.getCompoundName(self.compound1Id)
		self.assertEqual(name, compound1['name'])

class GetCompoundFormulaTestCase(GetBaseTestCase):
	def runTest(self):
		formula = dbHandle.getCompoundFormula(self.compound1Id)
		self.assertEqual(formula, compound1['formula'])

class GetCompoundIUPACnameTestCase(GetBaseTestCase):
	def runTest(self):
		IUPACname = dbHandle.getCompoundIUPACname(self.compound1Id)
		self.assertEqual(IUPACname, compound1['iupac'])

class GetFingerprintTestCase(GetBaseTestCase):
	def runTest(self):
		fpId = dbHandle.getFingerprint(self.compound1Id)
		self.assertEqual(fpId, 1)

class GetFPStrTestCase(GetBaseTestCase):
	def runTest(self):
		fp = dbHandle.getFPStr(self.compound1Id)
		self.assertEqual(fp, compound1['fp'])

class GetRelationReactantTestCase(GetBaseTestCase):
	def runTest(self):
		reactantId = dbHandle.getReactantRelations(self.relation1Id)
		self.assert_(reactantId, self.compound2Id)

class GetRelationProductTestCase(GetBaseTestCase):
	def runTest(self):
		productId = dbHandle.getReactantRelations(self.relation1Id)
		self.assert_(productId, self.compound1Id)

class GetReactantProductsTestCase(GetBaseTestCase):
	def runTest(self):
		listOfProductIds = dbHandle.getReactantProducts(self.compound2Id)
		self.assert_(self.compound1Id in listOfProductIds)

class GetProductReactantsTestCase(GetBaseTestCase):
	def runTest(self):
		listOfReactantIds = dbHandle.getProductReactants(self.compound1Id)
		self.assert_(self.compound2Id in listOfReactantIds)
		self.assert_(self.compound3Id in listOfReactantIds)

class GetReferenceAuthorTestCase(GetBaseTestCase):
	def runTest(self):
		author = dbHandle.getReferenceAuthor(self.reference1Id)
		self.assertEqual(author, reference1['author'])

class GetReferenceNameTestCase(GetBaseTestCase):
	def runTest(self):
		name = dbHandle.getReferenceName(self.reference1Id)
		self.assertEqual(name, reference1['name'])

class GetReferenceYearTestCase(GetBaseTestCase):
	def runTest(self):
		year = dbHandle.getReferenceYear(self.reference1Id)
		self.assertEqual(year, reference1['year'])

class GetReferenceSourceTestCase(GetBaseTestCase):
	def runTest(self):
		source = dbHandle.getReferenceSource(self.reference1Id)
		self.assertEqual(source, reference1['source'])

class GetRelationReactantTestCase(GetBaseTestCase):
	def runTest(self):
		reactantId = dbHandle.getRelationReactant(self.relation1Id)
		self.assertEqual(reactantId, self.compound2Id)

class GetRelationProductTestCase(GetBaseTestCase):
	def runTest(self):
		productId = dbHandle.getRelationProduct(self.relation1Id)
		self.assertEqual(productId, self.compound1Id)

class GetRelationReactionTestCase(GetBaseTestCase):
	def runTest(self):
		reactionId = dbHandle.getRelationReaction(self.relation1Id)
		self.assertEqual(reactionId, self.reaction1Id)

class GetReactionReferenceTestCase(GetBaseTestCase):
	def runTest(self):
		referenceId = dbHandle.getReactionReference(self.reaction1Id)
		self.assertEqual(referenceId, self.reference1Id)

class GetReactionTempTestCase(GetBaseTestCase):
	def runTest(self):
		temp = dbHandle.getReactionTemp(self.reaction1Id)
		self.assertEqual(temp, reaction1['temp'])

class GetReactionSolventTestCase(GetBaseTestCase):
	def runTest(self):
		solvent = dbHandle.getReactionSolvent(self.reaction1Id)
		self.assertEqual(solvent, reaction1['solvent'])

class GetReactionStateTestCase(GetBaseTestCase):
	def runTest(self):
		state = dbHandle.getReactionState(self.reaction1Id)
		self.assertEqual(state, reaction1['state'])

class GetReactionPressureTestCase(GetBaseTestCase):
	def runTest(self):
		state = dbHandle.getReactionPressure(self.reaction1Id)
		self.assertEqual(state, reaction1['pressure'])

class GetReactionPressureTestCase(GetBaseTestCase):
	def runTest(self):
		state = dbHandle.getReactionPressure(self.reaction1Id)
		self.assertEqual(state, reaction1['pressure'])

class GetReactionTimeTestCase(GetBaseTestCase):
	def runTest(self):
		time = dbHandle.getReactionTime(self.reaction1Id)
		self.assertEqual(time, reaction1['time'])

class GetReactionPHTestCase(GetBaseTestCase):
	def runTest(self):
		pH = dbHandle.getReactionPH(self.reaction1Id)
		self.assertEqual(pH, reaction1['pH'])

class GetReactionOtherTestCase(GetBaseTestCase):
	def runTest(self):
		other = dbHandle.getReactionOther(self.reaction1Id)
		self.assertEqual(other, reaction1['other'])

class GetReactionCatalystsTestCase(GetBaseTestCase):
	def runTest(self):
		catalysts = dbHandle.getReactionCatalysts(self.reaction1Id)
		self.assert_(self.catalyst1Id in catalysts, "catalysts: "+str(catalysts))
		self.assert_(self.catalyst2Id in catalysts, "catalysts: "+str(catalysts))

class GetReactionRelationsTestCase(GetBaseTestCase):
	def runTest(self):
		relations = dbHandle.getReactionRelations(self.reaction1Id)
		self.assert_(self.relation1Id in relations, "relations: "+str(relations))
		self.assert_(self.relation2Id in relations, "relations: "+str(relations))

class GetCatalystNameTestCase(GetBaseTestCase):
	def runTest(self):
		name = dbHandle.getCatalystName(self.catalyst1Id)
		self.assertEqual(name, catalyst1['name'])

class GetCatalystReactionTestCase(GetBaseTestCase):
	def runTest(self):
		reactionId = dbHandle.getCatalystReaction(self.catalyst1Id)
		self.assertEqual(reactionId, self.reaction1Id)

if __name__=="__main__":
	unittest.main()